#include "StandardItemModel.h"
#include <QDebug>




StandardItemModel::StandardItemModel(QObject *parent)
    : QStandardItemModel(parent)
{

}

QVariant StandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
        {
            if (orientation == Qt::Horizontal)
            {
                return m_header[section];
            }
        }

        return QVariant();
}

QModelIndex StandardItemModel::index(int row, int column, const QModelIndex &parent) const
{
    return this->item(row,column)->index();
}

//QModelIndex StandardItemModel::parent(const QModelIndex &index) const
//{
//    // FIXME: Implement me!
//    return QStandardItemModel::itemFromIndex(index)->index();
//}



QVariant StandardItemModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    if(role==Qt::DisplayRole){
        return hash[index.row()].value(index.column());
    }
    return QVariant();
}

void StandardItemModel::importData()
{
    //QFile aFile(":/resource/csv/Data.csv");
    QFile aFile("G:/MyApp/projectSelf/bin_release/resource/csv/Data.csv");


    QStringList fFileContent;
    if (aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream aStream(&aFile);

        while (!aStream.atEnd())
        {
            QString str=aStream.readLine();//读取文件的一行
            fFileContent.append(str); //添加到 StringList
        }
        //qDebug()<<fFileContent;

        aFile.close();//关闭文件

        iniModelFromStringList(fFileContent);//从StringList的内容初始化数据模型
    }
}

QHash<int,QVariantList> StandardItemModel::getModel()
{
    QFile aFile("G:/MyApp/projectSelf/bin_release/resource/csv/Data.csv");


    QStringList fFileContent;
    if (aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream aStream(&aFile);

        while (!aStream.atEnd())
        {
            QString str=aStream.readLine();//读取文件的一行
            fFileContent.append(str); //添加到 StringList
        }

        aFile.close();//关闭文件

    }
    QStandardItemModel *model =new QStandardItemModel();

    model->clear();
    int rowCnt=fFileContent.count(); //文本行数，第1行是标题
    this->setRowCount(rowCnt-1); //实际数据行数
    //设置表头
    QString header=fFileContent.at(0);//第1行是表头
    QStringList headerList=header.split(QRegExp(","),QString::SkipEmptyParts);
    qDebug()<<"Title OK!";

    //设置表格数据
    QString aText;
    QStringList tmpList;
    int j;
    QStandardItem   *aItem;
    for (int i=1;i<rowCnt;i++)
    {
        QString aLineText=fFileContent.at(i); //获取数据区的一行
        QStringList tmpList=aLineText.split(QRegExp(","),QString::SkipEmptyParts);
        for (j=0;j<headerList.count();j++)
        { //不包含最后一列
            aItem=new QStandardItem(tmpList.at(j));//创建item
            //aItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            model->setItem(i-1,j,aItem);
        }
    }
    return modelToHash(model);
}

QHash<int,QVariantList> StandardItemModel::modelToHash(QStandardItemModel *model)
{
    int i,j;
    QStandardItem   *aItem;
    QList<QVariant> row;


    //获取数据区文字
    for ( i=0;i<model->rowCount();i++)
    {
        for( j=0;j<model->columnCount();j++)
        {
            aItem=model->item(i,j);
            row.push_back(aItem->text());
        }
        //最后一列不需要加,
        //aItem=model->item(i,j);

        hash.insert(i,row);
        row.clear();
    }
    qDebug()<<":"<<hash;
    return hash;
}


void StandardItemModel::iniModelFromStringList(QStringList& aFileContent)
{
    this->clear();
    int rowCnt=aFileContent.count(); //文本行数，第1行是标题
    this->setRowCount(rowCnt-1); //实际数据行数
    //设置表头
    QString header=aFileContent.at(0);//第1行是表头
    QStringList headerList=header.split(QRegExp(","),QString::SkipEmptyParts);
    qDebug()<<"Title OK!";

    //设置表格数据
    QString aText;
    QStringList tmpList;
    int j;
    QStandardItem   *aItem;
    for (int i=1;i<rowCnt;i++)
    {
        QString aLineText=aFileContent.at(i); //获取数据区的一行
        QStringList tmpList=aLineText.split(QRegExp(","),QString::SkipEmptyParts);
        for (j=0;j<headerList.count();j++)
        { //不包含最后一列
            aItem=new QStandardItem(tmpList.at(j));//创建item
            //aItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            this->setItem(i-1,j,aItem);
            qDebug()<<"Set OK!";
        }
    }
    qDebug()<<"insert Successed!";
}

